package org.dochandler.service.core;

import java.io.Serializable;
import java.util.List;

import org.dochandler.dao.DocumentDAO;
import org.dochandler.dao.OfficerDAO;
import org.dochandler.service.OfficerService;
import org.dochandler.service.domain.Document;
import org.dochandler.service.domain.Officer;
import org.dochandler.service.domain.annotation.StateOfWar;
import org.dochandler.service.exception.DocumentNotFoundException;
import org.esfinge.guardian.abac.annotation.authorization.Rule;
import org.esfinge.guardian.utils.ServiceLocator;

@SuppressWarnings("serial")
public class OfficerServiceCore implements OfficerService, Serializable {
	
	private DocumentDAO documentDAO;
	private OfficerDAO officerDAO;
	private Officer officer;
	
	public OfficerServiceCore(Officer officer) {
		setDocumentDAO( ServiceLocator.getServiceImplementation(DocumentDAO.class) );
		setOfficerDAO( ServiceLocator.getServiceImplementation(OfficerDAO.class) );
		setOfficer(officer);
	}
	
	@Override
	@StateOfWar
	public Document accessDocument(Long documentId) {
		Document doc = documentDAO.findById(documentId);
			
		if (doc == null) {
			String msg = "Could not find document: " + documentId; 
			throw new DocumentNotFoundException(msg);
		}
		
		return doc;
	}
	
	@Override
	@Rule("2 > 3")
	public List<Document> listDocuments() {
		return documentDAO.findAll();
 	}
	
	@Override
	public boolean createDocument(Document document) {
		return documentDAO.create(document);
	}

	@Override
	@StateOfWar
	public boolean createAttackDocument(Document document) {
		return false;
	}
	
	/*
	 * SETTERS AND GETTERS
	 */
	
	public Officer getOfficer() {
		return officer;
	}
	public void setOfficer(Officer officer) {
		this.officer = officer;
	}
	public DocumentDAO getDocumentDAO() {
		return documentDAO;
	}
	public void setDocumentDAO(DocumentDAO documentDAO) {
		this.documentDAO = documentDAO;
	}
	public OfficerDAO getOfficerDAO() {
		return officerDAO;
	}
	public void setOfficerDAO(OfficerDAO officerDAO) {
		this.officerDAO = officerDAO;
	}
}